home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / script.lzh / script_0.4 / example / host.c < prev    next >
C/C++ Source or Header  |  1996-12-17  |  3KB  |  146 lines

  1. /*
  2.    script.library
  3.    host example
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #include <dos/dos.h>
  10. #include <rexx/rxslib.h>
  11. #include <rexx/storage.h>
  12.  
  13. #include <proto/dos.h>
  14. #include <proto/exec.h>
  15. #include <proto/rexxsyslib.h>
  16.  
  17. #if defined (__SASC)
  18. #include "/script.h"
  19. #include "/script_pragmas.h"
  20. #elif defined (__GNUC__)
  21. #include "../script_inline.h"
  22. #endif
  23.  
  24. #include "example.h"
  25.  
  26. #if defined (__GNUC__)
  27. #define RXSLIB struct RxsLib
  28. #elif defined (__SASC)
  29. #define RXSLIB struct Library
  30. #endif
  31.  
  32. RXSLIB *RexxSysBase = NULL;
  33. struct Library *ScriptBase = NULL;
  34. struct MsgPort *MyPort = NULL;
  35.  
  36. char *port_name = PORT_NAME,
  37.      *func_name = FUNC_NAME,
  38.      *variable_name  = VAR_NAME,
  39.      *variable_value,
  40.      *return_value = RET_VALUE;
  41.  
  42. void
  43. Cleanup (void)
  44. {
  45.   struct Message *msg;
  46.  
  47.   if (MyPort)
  48.   {
  49.     Forbid ();
  50.     RemPort (MyPort);
  51.     Permit ();
  52.     while (msg = GetMsg (MyPort))
  53.       ReplyMsg (msg);
  54.     DeleteMsgPort (MyPort);
  55.   }
  56.   if (RexxSysBase)
  57.   {
  58.     CloseLibrary ((struct Library *) RexxSysBase);
  59.   }
  60.   if (ScriptBase)
  61.   {
  62.     CloseLibrary (ScriptBase);
  63.   }
  64. }
  65.  
  66. void
  67. Mainloop (void)
  68. {
  69.   struct RexxMsg *msg;
  70.   ULONG mask;
  71.  
  72.   mask = 1 << MyPort -> mp_SigBit;
  73.  
  74.   for (;;)
  75.   {
  76.     if (Wait (SIGBREAKF_CTRL_C | mask) & SIGBREAKF_CTRL_C)
  77.       break;
  78.  
  79.     while (msg = (struct RexxMsg *) GetMsg (MyPort))
  80.     {
  81.       Script_GetRexxVar (msg, variable_name, &variable_value);
  82.       PutStr (variable_name);
  83.       PutStr (" = ");
  84.       if (variable_value)
  85.         PutStr (variable_value);
  86.       else
  87.         PutStr ("<null>");
  88.       PutStr ("\n");
  89.  
  90.       Script_SetRexxVar (msg, variable_name, return_value);
  91.       msg -> rm_Result1 = 0;
  92.  
  93.       ReplyMsg ((struct Message *) msg);
  94.     }
  95.   }
  96. }
  97.  
  98. int
  99. main (int argc, char *argv[])
  100. {
  101.   atexit (Cleanup);
  102.  
  103.   if (argc >= 2)
  104.     port_name = argv[1];
  105.   if (argc >= 3)
  106.     func_name = argv[2];
  107.   if (argc >= 4)
  108.     variable_name = argv[3];
  109.   if (argc >= 5)
  110.     return_value = argv[4];
  111.  
  112.   if (!(ScriptBase = OpenLibrary ("script.library", 0)))
  113.   {
  114.     PutStr ("Can't open script.library.\n");
  115.     return 20;
  116.   }
  117.   if (!(RexxSysBase = (RXSLIB *) OpenLibrary ("rexxsyslib.library", 36)))
  118.   {
  119.     PutStr ("Can't open rexxsys.library.\n");
  120.     return 20;
  121.   }
  122.   if (!(MyPort = CreateMsgPort ()))
  123.   {
  124.     PutStr ("Can't create port.\n");
  125.     exit (20);
  126.   }
  127.   MyPort -> mp_Node.ln_Name = port_name;
  128.   MyPort -> mp_Node.ln_Pri  = 0;
  129.  
  130.   Forbid ();
  131.   if (FindPort (port_name))
  132.   {
  133.     Permit ();
  134.     PutStr ("Port already in use.\n");
  135.     DeleteMsgPort (MyPort);
  136.     MyPort = NULL;
  137.     exit (20);    
  138.   }
  139.   AddPort (MyPort);
  140.   Permit ();
  141.  
  142.   Mainloop ();
  143.   PutStr ("Done\n");
  144.   exit (0);
  145. }
  146.